import { personnelGroupGetByWorkshopIds, spinProcessGetByWorkshopIds } from '../../../api/system/user';

export default {
	methods: {
		onCheckedChange(e, row, type) {
			if (type === 'workshop') {
				this.workshopList.forEach(x => {
					if (!e) {
						if (row.id === x.id && x.isDefault) {
							x.isDefault = false;
						}
					}
					return x;
				});
				this.getProcessListByWorkshopsRequest();
				this.getGroupListByWorkshopsRequest();
			} else if (type === 'group') {
				this.groupList.forEach(x => {
					if (!e) {
						if (row.id === x.id && x.isDefault) {
							x.isDefault = false;
						}
					}
					return x;
				});
				this.checkedGroupArr = this.groupList.filter(x => x.checked);
			} else if (type === 'process') {
				this.processList.forEach(x => {
					if (!e) {
						if (row.id === x.id && x.isDefault) {
							x.isDefault = false;
						}
					}
				});
				this.checkedProcessArr = this.processList.filter(x => x.checked);
			}
		},
		onDefaultChange(e, row, type) {
			if (type === 'workshop') {
				this.workshopList = this.workshopList.map(x => {
					if (row.id !== x.id) x.isDefault = false;
					if (e && row.id === x.id) x.checked = true;
					return x;
				});
				this.getProcessListByWorkshopsRequest();
				this.getGroupListByWorkshopsRequest();
			} else if (type === 'group') {
				this.groupList = this.groupList.map(x => {
					if (row.id !== x.id) x.isDefault = false;
					if (e && row.id === x.id) x.checked = true;
					return x;
				});
				this.checkedGroupArr = this.groupList.filter(x => x.checked);
			} else if (type === 'process') {
				this.processList = this.processList.map(x => {
					if (row.id !== x.id) x.isDefault = false;
					if (e && row.id === x.id) x.checked = true;
					return x;
				});
				this.checkedProcessArr = this.processList.filter(x => x.checked);
			}
		},
		// 根据区域集合获取工序列表
		getProcessListByWorkshopsRequest() {
			const arr = this.workshopList.filter(x => x.checked);
			const ids = arr.map(x => x.id);
			if (!ids || !ids.length) {
				this.processList = [];
				return;
			}
			return spinProcessGetByWorkshopIds({
				ids
			}).then(res => {
				if (res.data.status === 200) {
					const arr = res.data.res || [];
					this.processList = arr.map(x => {
						return {
							id: x.id,
							code: x.code,
							name: x.name,
							checked: this.checkedProcessArr.some(item => item.id === x.id),
							isDefault: this.checkedProcessArr.some(item => item.id === x.id && item.isDefault)
						};
					});
				}
			});
		},
		// 根据区域集合获取班组列表
		getGroupListByWorkshopsRequest() {
			const arr = this.workshopList.filter(x => x.checked);
			const ids = arr.map(x => x.id);
			if (!ids || !ids.length) {
				this.groupList = [];
				return;
			}
			return personnelGroupGetByWorkshopIds({
				ids
			}).then(res => {
				if (res.data.status === 200) {
					const arr = res.data.res || [];
					this.groupList = arr.map(x => {
						return {
							id: x.id,
							code: x.code,
							name: x.name,
							checked: this.checkedGroupArr.some(item => item.id === x.id),
							isDefault: this.checkedGroupArr.some(item => item.id === x.id && item.isDefault)
						};
					});
				}
			});
		}
	}
};
